﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using PMS.BusinessFacade;
using PMS.EntityManage;
using PMS.Common;
using PMS.EntityQuery;
using System.Data.SqlClient;
using System.Collections.Generic;
using BusinessFacade;
using EntityManage;

public partial class Modules_Personal_PerfEvaluateOrgPersonalReport : System.Web.UI.Page
{
    IReportHandler rptHandler = new ReportManage();
    IPerf_Personal_ApprovalHandler approvalHandler = new Perf_Personal_ApprovalManage();
    public ISys_UserHandler userHandler = new Sys_UserManage();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ViewState["OrderBy"] = "Id";
            ViewState["Accending"] = "desc";
            ViewState["NowYear"] = DateTime.Now.Year;
            ViewState["NowMonth"] = DateTime.Now.Month;
            SqlParameter[] parms = null;
            string clause = " 1=1 ";
            FillSearchDDL();
            ProcessParms(ref clause, ref parms);
            dataBind(clause, parms);

        }
    }
    /// <summary>
    /// 绑定季度绩效成绩
    /// </summary>
    private void dataBind(string whereClause, SqlParameter[] parms)
    {
        try
        {

        SqlParameter[] parms2 = null;
        int counter = 0;
        DataTable dt = new DataTable();

        if (whereClause != string.Empty)
        {
            //调用ReportManage写好的方法绑定数据
            dt = rptHandler.GetPerfEvaluateOrgSeasonDataByPageList(whereClause, parms, AspNetPager.StartRecordIndex - 1, AspNetPager.PageSize);
            whereClause = "1=1";
            ProcessParms(ref whereClause, ref parms2);
            //调用ReportManage写好的方法绑定数量
            counter = rptHandler.GetSeasonDataCounter(whereClause, parms2);
        }

        this.AspNetPager.RecordCount = counter;
        this.gvPersonalList.DataSource = dt;
        this.gvPersonalList.DataBind();
        }
        catch
        {
            Jscript.AlertInAjax("連接錯誤!", UpdatePanel1);
            return;
        }
    }
    /// <summary>
    /// 条件参数处理
    /// </summary>
    /// <param name="whereClause"></param>
    /// <param name="parms"></param>
    private void ProcessParms(ref string whereClause, ref SqlParameter[] parms)
    {
        List<SqlParameter> lstParm = new List<SqlParameter>();

        //获取当前季度
        if (int.Parse(ViewState["NowMonth"].ToString()) >= 1 && int.Parse(ViewState["NowMonth"].ToString()) <= 3)
        {
            whereClause += " and begin_time between @BegionTime and @EndTime ";
            lstParm.Add(new SqlParameter("@BegionTime", ViewState["NowYear"] + "/01/01"));
            lstParm.Add(new SqlParameter("@EndTime", ViewState["NowYear"] + "/03/31"));
        }
        else if (int.Parse(ViewState["NowMonth"].ToString()) >= 4 && int.Parse(ViewState["NowMonth"].ToString()) <= 6)
        {
            whereClause += " and begin_time between @BegionTime and @EndTime ";
            lstParm.Add(new SqlParameter("@BegionTime", ViewState["NowYear"] + "/04/01"));
            lstParm.Add(new SqlParameter("@EndTime", ViewState["NowYear"] + "/06/30"));
        }
        else if (int.Parse(ViewState["NowMonth"].ToString()) >= 6 && int.Parse(ViewState["NowMonth"].ToString()) <= 9)
        {
            whereClause += " and begin_time between @BegionTime and @EndTime ";
            lstParm.Add(new SqlParameter("@BegionTime", ViewState["NowYear"] + "/07/01"));
            lstParm.Add(new SqlParameter("@EndTime", ViewState["NowYear"] + "/09/30"));
        }
        else if (int.Parse(ViewState["NowMonth"].ToString()) >= 10 && int.Parse(ViewState["NowMonth"].ToString()) <= 12)
        {
            whereClause += " and begin_time between @BegionTime and @EndTime ";
            lstParm.Add(new SqlParameter("@BegionTime", ViewState["NowYear"] + "/10/01"));
            lstParm.Add(new SqlParameter("@EndTime", ViewState["NowYear"] + "/12/31"));
        }
        parms = lstParm.ToArray();
    }
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        //查询 
        ViewState["NowMonth"] = ddlSeason.SelectedValue.ToString();
        SqlParameter[] parms = null;
        string clause = " 1=1 ";

        ProcessParms(ref clause, ref parms);
        dataBind(clause, parms);
    }
    //绑定查询的下拉列表
    private void FillSearchDDL()
    {
        DataTable dtSeason = new DataTable();
        dtSeason.Columns.Add("Value");
        dtSeason.Columns.Add("Name");
        DataRow drSeason;

        drSeason = dtSeason.NewRow();
        drSeason[0] = DateTime.Now.Month.ToString();
        drSeason[1] = "";
        dtSeason.Rows.Add(drSeason);

        drSeason = dtSeason.NewRow();
        drSeason[0] = "1";
        drSeason[1] = "第一季度";
        dtSeason.Rows.Add(drSeason);

        drSeason = dtSeason.NewRow();
        drSeason[0] = "4";
        drSeason[1] = "第二季度";
        dtSeason.Rows.Add(drSeason);

        drSeason = dtSeason.NewRow();
        drSeason[0] = "7";
        drSeason[1] = "第三季度";
        dtSeason.Rows.Add(drSeason);

        drSeason = dtSeason.NewRow();
        drSeason[0] = "10";
        drSeason[1] = "第四季度";
        dtSeason.Rows.Add(drSeason);

        this.ddlSeason.DataSource = dtSeason;
        ddlSeason.DataTextField = "Name";
        ddlSeason.DataValueField = "Value";
        ddlSeason.DataBind();
    }
    protected void gvPersonalList_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "GetByName")//点击人员名称跳转到所有季度绩效
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), System.Guid.NewGuid().ToString(), string.Format("window.location.href='PerfEvaluateOrgPersonalAllSeasonReport.aspx?UserId={0}'", e.CommandArgument.ToString()), true);
            //Response.Redirect("PerfEvaluateOrgPersonalAllSeasonReport.aspx?UserId=" + e.CommandArgument.ToString());
        }
        if (e.CommandName == "GetByScore")//点击分数跳转到该成绩的指标类型得分、指标得分
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), System.Guid.NewGuid().ToString(), string.Format("window.location.href='PerfEvaluateOrgPersonalTargetTypeandTargetScore.aspx?UserId={0}&Month=" + ddlSeason.SelectedValue.ToString() + "'", e.CommandArgument.ToString()), true);
        }
    }
    protected void gvPersonalList_Sorting(object sender, GridViewSortEventArgs e)
    {

    }
    protected void gvPersonalList_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            
        }
    }
    protected void ApprovalButton_Click(object sender, EventArgs e)
    {
        List<string> ids = new List<string>();
        for (int i = 0; i < gvPersonalList.Rows.Count; i++)
        {
            if (this.gvPersonalList.Rows[i].RowType == DataControlRowType.DataRow)
            {
                HtmlInputCheckBox chkb = (HtmlInputCheckBox)gvPersonalList.Rows[i].FindControl("chkSelected");
                if (chkb.Checked)
                    ids.Add(chkb.Value);
            }
        }
        string userId = string.Empty;
        if (ids.Count > 0)
        {
            for (int i = 0; i < ids.Count; i++)
            {
                if (i == ids.Count - 1)
                {
                    userId += ids[i];
                }
                else
                {
                    userId += ids[i] + ",";
                }
            }
        }
        Response.Redirect("PerfEvaluateOrgPersonalDetail.aspx?UserId="+userId);

    }
    protected void AspNetPager_PageChanged(object sender, EventArgs e)
    {

    }
}
